<?php
$s = "SESSION_TYPE, SUBJECT, LPAD(CATALOG_NBR,3,0) AS CATALOG_NBR, CLASS_SECTION, CLASS_NBR, COURSE_TITLE_LONG, CREDITS, LOCATION, STND_MTG_PAT, STIME, ETIME, INSTRUCTORNAME, ENRL_TOT, ENRL_CAP, TERM, STRM, NOTETEXT, FACILITY_ID, SDATE, $order_locations AS locationorder";

$ctsql = "SELECT COUNT(*) FROM {$tname}{$whereclause}";
if($ctsql = mysql_query($ctsql,$GLOBALS['lflink'])) {
	if($tt = mysql_fetch_row($ctsql)) {
		$total = $tt[0];
	}
}

unset($l,$ctsql,$tt);

$topclause = "SELECT {$s} FROM {$tname}{$whereclause}{$orderby} LIMIT {$start},{$howmany}";

$s = $topclause;

if($s = mysql_query($s,$GLOBALS['lflink'])) {
	$results = true;
	$i=0;
	$t=0;
	$tn = 0;
	ob_start();
	while($r = mysql_fetch_assoc($s)) {
		if(!isset($t) || $r['STRM'] != $t) {
			if(isset($l)) {
				echo '
	</tbody>
</table>';
				unset($l);
			}
			$t = $r['STRM'];
			echo '
		<h2>Classes being offered during the <em>'.$terms[$t].'</em> academic term</h2>';
		}
		if(!isset($l) || $r['LOCATION'] != $l) {
			if(isset($l)) {
				echo '
	</tbody>
</table>';
				$i=0;
			}
			$tn++;
			echo '
<table id="'.strtolower($r['LOCATION']).'-'.$t.'" class="courselist';
			echo ($tn%2) ? ' maintable' : ' alttable';
			echo '">';
			echo '
	<caption>'.$course_locations[strtoupper($r['LOCATION'])].'</caption>';
			echo '
	<thead>
		<tr>
			<td class="course-checkbox" style="border-top: 1px solid #fff;border-left: 1px solid #fff;">&nbsp;</td>
			<th>Course Nbr.</th>
			<th>Subject, Section</th>
			<th>Title</th>
			<th>Credits</th>
			<th>Days</th>
			<th>Times</th>
			<th>Instructor</th>
			<th>Building</th>
			<th>Seats Left</th>
		</tr>
	</thead>
	<tbody>';
			$l = $r['LOCATION'];
		}
		$c = new sis_course($r);
		$c->formatvalues();
		$cd = new listedcourse;
		$cdinfo = trim($c->subject).' '.trim($c->catalog_nbr).'-'.trim($c->class_section);
		$cdinfo = $cd->findcourselinks($cdinfo);
		unset($cd);
		$coursetime = (!empty($c->stime)) ? $c->stime : '';
		$coursetime .= (!empty($c->stime) && !empty($c->etime)) ? ' to ' : '&nbsp;';
		$coursetime .= (!empty($c->etime)) ? $c->etime : '';
		$iname = explode(',',$c->instructorname);
		$c->instructorname = implode(', ',$iname);
		unset($iname);
		$c->class_section = trim($c->class_section);
		if(strtoupper($c->class_section[0]) == 'W') {
			if(emptyreturn(trim($c->stnd_mtg_pat))) {
				$c->stnd_mtg_pat = $GLOBALS['course_days']['VIRT'];
			}
			if(empty($c->stime) && empty($c->etime)) {
				$coursetime = $GLOBALS['course_days']['VIRT'];
			}
		}
		$c->facility_id = trim($c->facility_id);
		if(!empty($c->facility_id)) {
			$b = substr($c->facility_id,0,4);
			$bldg = (array_key_exists($b,$course_bldgs)) ? $course_bldgs[$b] : $c->facility_id;
		}
		else {
			$bldg = '&nbsp;';
		}
		echo '
		<tr class="row'.($i%2).'">
			<td class="course-checkbox"';
		echo (!empty($c->notetext)) ? ' rowspan="2"' : '';
		echo '><input type="checkbox" name="chosen-courses['.$c->class_nbr.']" id="chosen-courses_'.$c->class_nbr.'" /></td>
			<th scope="row"';
		echo (!empty($c->notetext)) ? ' rowspan="2"' : '';
		echo '>'.$c->class_nbr.'</th>
			<td>'.$cdinfo.'</td>
			<td>'.$c->course_title_long.'</td>
			<td>'.$c->credits.'</td>
			<td>'.$c->stnd_mtg_pat.'</td>
			<td>'.$coursetime/*.'</td>
			<td>'.formatDate($c->sdate,0)*/.'</td>
			<td>'.$c->instructorname.'</td>
			<td>'.$bldg.'</td>
			<td>';
		echo (($c->enrl_cap-$c->enrl_tot) > 0) ? ($c->enrl_cap-$c->enrl_tot) : 'Closed';
		echo '</td>';
		unset($cdinfo);
		unset($coursetime);
		if(!empty($c->notetext)) {
			$tmpnote = preg_replace('`([a-zA-Z0-9\!#$%&\'\*\+-/=\?\^_\`\{\|\}\~\.]+?)@([a-zA-Z0-9-\.]+?)\.([a-zA-Z0-9]{2,4})`i','<a href="mailto:$1@$2.$3">$1@$2.$3</a>',$c->notetext);
#					$tmpnote = preg_replace('`http://([^\s|\<]+?)`is','<a href="$1">$1</a>',$tmpnote);
			$tmpnote = make_clickable($tmpnote);
			echo '
		</tr>
		<tr class="row'.($i%2).' noterow">
			<th scope="row">Notes:</th>
			<td colspan="7">'.$tmpnote.'</td>';
			unset($tmpnote);
		}
		echo '
		</tr>';
		unset($c);
		$i++;
	}
	if(isset($l) && $l !== 0) {
		echo '
	</tbody>
</table>';
	}
	$ctable = ob_get_contents();
	ob_end_clean();
}
else {
	die('There was an SQL error. '.mysql_error()."\n$topclause");
}
mysql_free_result($s);
$prevlink = array();
$nextlink = array();
$qs = $_GET;
unset($qs['pageinfo'],$qs['id']);
$qs = json_encode($qs);

if($start != 0) {
	$prevlink['start'] = ($start < $howmany) ? 'start=0' : 'start='.($start-$howmany);
	if($howmany != DEFHM) {
		$prevlink['howmany'] = 'howmany='.$howmany;
	}
	foreach($_GET as $k=>$v) {
		if($k != 'pageinfo' && $k != 'id') {
			$prevlink[$k] = (is_array($_GET[$k])) ? $k.'[]='.implode('&amp;'.$k.'[]=',$_GET[$k]) : $k.'='.$v;
		}
	}
}
if(($start+$howmany) <= $total) {
	$nextlink['start'] = 'start='.($start+$howmany);
	if($howmany != DEFHM) {
		$nextlink['howmany'] = 'howmany='.$howmany;
	}
	foreach($_GET as $k=>$v) {
		if($k != 'pageinfo' && $k != 'id') {
			if(is_array($_GET[$k])) {
				$nextlink[$k] = $k.'[]='.implode('&amp;'.$k.'[]=',$_GET[$k]);
			}
			else {
				$nextlink[$k] = $k.'='.$v;
			}
		}
	}
}

if($total > 0) {
	ob_start();
	require_once($GLOBALS['paths']['htmlpath'].'sis-courses/sis-pagination.inc.php');
	$pag = ob_get_contents();
	ob_end_clean();
}
else {
	$pag = '';
}
echo (!empty($pag)) ? '<div id="'.$hashname.'"><div class="toppagination">'.$pag.'<div class="cleared">&nbsp;</div></div>'.$ctable.'<div class="botpagination">'.$pag.'<div class="cleared">&nbsp;</div></div>'.((isset($qs)) ? '<div class="cacheparams">'.$qs.'</div>' : '').'</div>' : $ctable;
unset($pag,$ctable);
?>